import { Component, Injector } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { FileUploadServiceProxy, UpdateFileNameInput } from '@shared/service-proxies/service-proxies';
import { BsModalRef } from 'ngx-bootstrap/modal';
import { merge as _merge } from 'lodash-es';
import { finalize } from 'rxjs/operators';

export interface IFileRenameModalOptions {
    originName: string;
    id: string;
}

@Component({
    templateUrl: './file-rename-modal.component.html'
})
export class FileRenameModalComponent extends AppComponentBase {

    reloadList: {():void};

    options: IFileRenameModalOptions ;

    saving: boolean = false;

    data: UpdateFileNameInput;

    originName: string;


    constructor(
        injector: Injector,
        public bsModalRef: BsModalRef,
        private _fileUploadServiceProxy: FileUploadServiceProxy
    ) {
        super(injector);
        
        this.data = new UpdateFileNameInput();
    }

    configure(options: IFileRenameModalOptions): void {
        this.options = options;
        this.originName = this.options.originName;
        this.data.id = this.options.id;
    }

    close(): void {
        this.bsModalRef.hide();
    }

    shown(): void {
        
    }

    save() {
        if (this.saving) {
            return;
        }
        this.saving = true;

        this._fileUploadServiceProxy.updateFileName(this.data)
            .pipe(finalize(() => {
                this.saving = false;
            }))
            .subscribe(result => {
                this.notify.success(this.l('SavedSuccessfully'));
                this.reloadList && this.reloadList();
                this.close();
            });
    }
}
